﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;

namespace TotalLMS.Datamodels.cmi
{
    //TODO: what is the "implements Serializible" equivalent in C#
    //public class CMITime implements Serializable
    public class CMITime : ISerializable
    {
        private int CMIHours;
        private int CMIMinutes;
        private float CMISeconds;


        /***************************************************************************
        **
        ** Method:  constructor
        ** Input:   String timeString
        ** Output:  none
        **
        ** Description:
        ** This constructor takes as input a string that should be in the form of
        ** HH:MM:SS.S and create a CMITime object.
        ** For this example code error checking is not being done to ensure that the
        ** string is in the correct format.
        **
        ***************************************************************************/
        public CMITime(String timeString)
        {
            string[] st = timeString.Split(':');
            String lmsHours = st[0];
            String lmsMinutes = st[1];
            String lmsSeconds = st[2];

            CMIHours = int.Parse(lmsHours);
            CMIMinutes = int.Parse(lmsMinutes);
            CMISeconds = float.Parse(lmsSeconds);
            //CMISeconds = Float.parseFloat( lmsSeconds );
        }

        /************************************************************************
         **  Accessors to the CMITime Data.
         ************************************************************************/
        public int getHours()
        {
            return CMIHours;
        }
        public int getMinutes()
        {
            return CMIMinutes;
        }
        public float getSeconds()
        {
            return CMISeconds;
        }

        /************************************************************************
         **  Modifiers to the CMITime Data.
         ************************************************************************/
        public void setHours(int iHours)
        {
            CMIHours = iHours;
        }
        public void setMinutes(int iMinutes)
        {
            CMIMinutes = iMinutes;
        }
        public void setSeconds(float iSeconds)
        {
            CMISeconds = iSeconds;
        }

        /***************************************************************************
        **
        ** Method:  toString
        ** Input:   none
        ** Output:  String representation of time
        **
        ** Description:
        ** This method will return the time as a String.  The format of the time will be
        ** hh:mm:ss.SS
        **
        ***************************************************************************/
        public String toString()
        {
            String temp = "0";

            String hours = String.Format("{0,10:G}", CMIHours);

            if (hours.Length == 1)
            {
                hours = "0" + hours;
            }
            else if (hours.Length == 0)
            {
                hours = "00";
            }

            String minutes = String.Format("{0,10:G}", CMIMinutes);

            if (minutes.Length == 1)
            {
                minutes = "0" + minutes;
            }
            else if (minutes.Length == 0)
            {
                minutes = "00";
            }

            String seconds = String.Format("{0,10:G}", CMISeconds);

            if (seconds.Length == 1)
            {
                seconds = "0" + seconds;
            }
            else if (seconds.Length == 0)
            {
                seconds = "00";
            }

            return hours + ":" + minutes + ":" + seconds;
        }

        /***************************************************************************
        **
        ** Method:  add
        ** Input:   CMITime addTime - time to add to this time
        ** Output:  none
        **
        ** Description:
        ** This method will add the time represented by the addTime input parameter to
        ** the time represented by 'this' CMITime object.  The new time will be stored
        ** back into 'this' CMITime object.
        **
        ***************************************************************************/
        public void add(CMITime addTime)
        {
            int newHours = 0;
            int newMinutes = 0;
            float newSeconds = (float)0.0;

            // Add the time together
            newSeconds = CMISeconds + addTime.CMISeconds;
            if (newSeconds > 60.0)
            {
                newSeconds = newSeconds - (float)60.0;
                newMinutes = newMinutes + 1;
            }
            // ensure that seconds is in the following format: ss.ss
            newSeconds = (float)((int)(newSeconds * 100)) / (float)100.00;

            newMinutes = newMinutes + CMIMinutes + addTime.CMIMinutes;
            if (newMinutes > 60)
            {
                newMinutes = newMinutes - 60;
                newHours = newHours + 1;
            }
            newHours = newHours + CMIHours + addTime.CMIHours;

            // Store off the new time back into the lms core data
            CMIHours = newHours;
            CMIMinutes = newMinutes;
            CMISeconds = newSeconds;
        }

        #region ISerializable Members
        //TODO: how to implement this interface method
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
